jQuery(document).ready(function () {
    init();
});

function init() {

    jQuery("a.gallery-slider-image[rel=galleryslider]").fancybox({
	'overlayOpacity' : 0.95,
	'cyclic' : true,
	'titlePosition' : 'inside',
	'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
	    return '<div align="center">' + (currentIndex + 1) + ' / ' + currentArray.length + '</div><div align="center">' + title +'</div>';
	},
        'onComplete' : function(){ ga_galleryslider(); }
    });


	jQuery('#gallery-carousel').cycle({ 
		fx:     'scrollHorz',
		speed:  'fast',
		timeout: 0,
		next:   '.next', 
		prev:   '.previous',
		slideResize: 0,
		containerResize: 0,
		after: function(currSlideElement, nextSlideElement, options, forwardFlag) {
			jcarouselinit();

			if (jQuery(nextSlideElement).find("img.gallery-main-image").attr("data-href")) {
				jQuery(nextSlideElement).find("img.gallery-main-image").attr("src", jQuery(nextSlideElement).find("img.gallery-main-image").attr("data-href"));
				jQuery(nextSlideElement).find("img.gallery-main-image").load(function() {
					jQuery(nextSlideElement).find("img.placeholder-image").remove();
					jQuery(nextSlideElement).find("img.gallery-main-image").fadeIn();
					jQuery(nextSlideElement).find("img.gallery-main-image").removeAttr('data-href');
					
				});
				
			}
			
			
			var next_image_selector = '#main-image-' + options.nextSlide;

			if (jQuery(next_image_selector).find("img.gallery-main-image").attr("data-href")) {
				jQuery(next_image_selector).find("img.gallery-main-image").attr("src", jQuery(next_image_selector).find("img.gallery-main-image").attr("data-href"));
				jQuery(next_image_selector).find("img.gallery-main-image").load(function() {
				jQuery(next_image_selector).find("img.placeholder-image").remove();
				jQuery(next_image_selector).find("img.gallery-main-image").fadeIn();
					jQuery(next_image_selector).find("img.gallery-main-image").removeAttr('data-href');
				});
			}

		}
	});

}

function jcarouselinit() {
  var remainder = counter % 3;
  if (remainder == 0){
    //refresh google ad after third click
    refreshTag();
  }
  counter++;
  
  //check GA permission

  if (Drupal.settings.dc_admin['GA_ROLES_ENABLED']) {
    eachstep();
  }
    
}

function eachstep() {
  var pathname = window.location.pathname;
  // console.log(pathname);
  _gaq.push(['_trackPageview', pathname]);
  // console.log();
  
}

function ga_galleryslider() {
    //check GA permission
    if (Drupal.settings.dc_admin['GA_ROLES_ENABLED']) {
	var gallery_pathname = window.location.pathname;
	_gaq.push(['_trackPageview', gallery_pathname]);
    }
}
